#include <iostream>
#include <vector>

using namespace std;
/**
 * @brief
 * 1 <= ops.length <= 1000
ops[i] is "C", "D", "+", or a string representing an integer in the range [-3 * 104, 3 * 104].
For operation "+", there will always be at least two previous scores on the record.
For operations "C" and "D", there will always be at least one previous score on the record.
 *
 */
class Solution
{
public:
    int calPoints(vector<string> &ops)
    {
        int opsLen = ops.size();
        vector<int> ans;
        for (int i = 0; i < opsLen; i++)
        {
            if (ops[i] == "C")
            {
                ans.pop_back();
            }
            else if (ops[i] == "D")
            {
                ans.push_back(ans.back() * 2);
            }
            else if (ops[i] == "+")
            {
                ans.push_back(ans.back()+ ans[ans.size() - 2]);
            }
            else
            {
                ans.push_back(stoi(ops[i]));
            }
        }
        int res = 0;
        for (int a : ans)
        {
            //cout << a << " ";
            res += a;
        }
       // cout<<endl;
        return res;
    }

     int calPoints_2(vector<string>& ops) {
        int ret = 0;
        vector<int> points;
        for (auto &op : ops) {
            int n = points.size();
            switch (op[0]) {
                case '+':
                    ret += points[n - 1] + points[n - 2];
                    points.push_back(points[n - 1] + points[n - 2]);
                    break;
                case 'D':
                    ret += 2 * points[n - 1];
                    points.push_back(2 * points[n - 1]);
                    break;
                case 'C':
                    ret -= points[n - 1];
                    points.pop_back();
                    break;
                default:
                    ret += stoi(op);
                    points.push_back(stoi(op));
                    break;
            }
        }
        return ret;
    }
};
int main()
{
    Solution s;
    vector<string> ops = {"5", "-2", "4", "C", "D", "9", "+", "+"};
    cout << s.calPoints(ops) << endl;
    system("pause");
    return 0;
}
